package app.netant.bt.btcat.thread;

import app.netant.bt.btcats.BtCatPage;
import cn.lily.com.db.DBFactory;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

public class BtCatThread extends Thread {
	
	private BtCatPool pool;
	
	public void setParent(BtCatPool pool){
		this.pool = pool;
	}
	
	@Override
	public void run() {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		while(true){
			
			int idx = this.pool.getIndex();
			if(idx == -1){
				break;
			}
			
			String href = this.pool.getPageUrl()+idx+".html";
			
			System.out.println("第"+idx+"页数据开始处理！");
			
			try {
				BtCatPage page = new BtCatPage(href);
				page.analyse();
			} catch (Exception e) {
				e.printStackTrace();
				DBFactory.getThreadDB().executeUpdate("insert into ${schema}.NET_MARGNET_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						+ "values ('"+System.currentTimeMillis()+MathUtil.random(3)+"','"+href+"','1')");						
				continue;
			}
			
		}
		
		DBFactory.getThreadDB().free();
		
		System.out.println("线程停止........");
		
		this.pool.remove(this);
		
	}
	
}
